package com.winderinfo.yikaotianxia.ui.address;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class AddAddressActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.address_name)
    EditText addressName;
    @BindView(R.id.address_phone)
    EditText addressPhone;
    @BindView(R.id.address_address)
    EditText addressAddress;
    @BindView(R.id.address_moren)
    CheckBox addressMoren;
    private Presenter presenter;
    private Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;
    private String userID;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_addaddress;
    }

    @Override
    protected void initView() {
        presenter = new Presenter(this);
        spUtils = new SPUtils(this);
        //获取用户Id
        userID = spUtils.getString("UserID", "");
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.address_back, R.id.address_paocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.address_back:
                finish();
                break;
            case R.id.address_paocun:
                String addressname = addressName.getText().toString();
                String addressphone = addressPhone.getText().toString();
                String addressaddress = addressAddress.getText().toString();
                //判断空
                if (TextUtils.isEmpty(addressname) || TextUtils.isEmpty(addressphone) || TextUtils.isEmpty(addressaddress)) {
                    ToastUtil.showSuccess(this, "信息不能为空");
                    return;
                }
                //提交
                /**
                 * @name 收货人名称
                 * @phone 手机号
                 * @addr 地址
                 * @isdefault 是否默认地址0不默认1默认
                 * @userId 用户id
                 * */
                map.put("name", addressname);
                map.put("phone", addressphone);
                map.put("addr", addressaddress);
                if (addressMoren.isChecked()) {
                    map.put("isdefault", "1");
                } else {
                    map.put("isdefault", "0");
                }
                map.put("userId", "1");
                presenter.onPostRequest(Api.ADDRESS, map, RegisterBean.class);
                break;
        }
    }

    @Override
    public void onSuccess(Object data) {
        if (data instanceof RegisterBean) {
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, "添加成功");
            } else if (((RegisterBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, "添加失败");
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

    @OnClick(R.id.address_back)
    public void onViewClicked() {
        finish();
    }
}
